var BemfaHTTP = (function () {
    var token;
    var statusItem = undefined;
    function DEBUG_LOG(data) {
        return;
        console.log(data);
    }

    //构造函数
    function _bemfahttp() {
        var _this = this;
        //构造函数安全模式，避免创建时候丢掉new关键字
        if (_this instanceof _bemfahttp) {

            //共有属性, 方法
            _this.setToken = function (_token) {
                _this.token = _token;
            }

            _this.sendMessage = function (
                _topic,
                _msg,
                callback_success = function (data) { },
                callback_fail = function () { }) {

                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: 'https://api.bemfa.com/api/device/v1/data/3/push/post/',
                    data: {
                        uid: _this.token,
                        topic: _topic,
                        msg: _msg
                    },
                    success: function (data) {
                        callback_success(data);
                    },
                    error: function (xhr, textStatus) {
                        //mdui.alert('异常错误，请刷新重新');
                        callback_fail();
                    }
                });
            }

            _this.getMessage = function (
                _topic,
                _num=1,
                callback_success = function (data) { },
                callback_fail = function () { }) {
                    
                $.ajax({
                    type: 'GET',
                    dataType: 'json',
                    url: 'https://api.bemfa.com/api/device/v1/data/3/get/',
                    data: {
                        uid: _this.token,
                        topic: _topic,
                        num:_num
                    },
                    success: function (data) {
                        callback_success(data);
                    },
                    error: function (xhr, textStatus) {
                        //mdui.alert('异常错误，请刷新重新');
                        callback_fail();
                    }
                });
            }

        } else {
            return new _bemfahttp();
        }
    }

    //静态共有属性方法
    _bemfahttp.prototype = {
        constructor: _bemfahttp

    }

    return _bemfahttp;
})();

